﻿Public Class CADASTRO
    Function BuscaCep(ByVal cep As String) As Hashtable
        Dim ds As DataSet
        Dim _resultado As String
        Dim ht As System.Collections.Hashtable
        Try
            ds = New DataSet()
            ds.ReadXml("http://cep.republicavirtual.com.br/web_cep.php?cep=" + cep.Replace("-", "").Trim() + "&formato=xml")
            If Not IsNothing(ds) Then
                If (ds.Tables(0).Rows.Count > 0) Then
                    _resultado = ds.Tables(0).Rows(0).Item("resultado").ToString()
                    ht = New Hashtable
                    Select Case _resultado
                        Case "1"
                            CIDADETextBox.Text = ds.Tables(0).Rows(0).Item("cidade").ToString().Trim().ToUpper
                            BAIRROTextBox.Text = ds.Tables(0).Rows(0).Item("bairro").ToString().Trim().ToUpper
                            ENDTextBox.Text = ds.Tables(0).Rows(0).Item("tipo_logradouro").ToString().Trim().ToUpper + " " + ds.Tables(0).Rows(0).Item("logradouro").ToString().Trim().ToUpper
                            ENDTextBox.Enabled = True
                            BAIRROTextBox.Enabled = True
                            CIDADETextBox.Enabled = True
                        Case "2"
                            CIDADETextBox.Text = ds.Tables(0).Rows(0).Item("cidade").ToString().Trim().ToUpper
                            ENDTextBox.Enabled = True
                            BAIRROTextBox.Enabled = True
                            CIDADETextBox.Enabled = True
                    End Select
                End If
            End If
            Return ht
        Catch ex As Exception
            Throw New Exception("Falha ao Buscar o Cep" & vbCrLf & ex.ToString)
            ENDTextBox.Enabled = True
            CIDADETextBox.Enabled = True
            BAIRROTextBox.Enabled = True
            Return Nothing
        End Try
    End Function
    Private Sub CADASTRO_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Keys.Enter
                SendKeys.Send("{Tab}")
        End Select
    End Sub
    Private Sub CADASTRO_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.CADASTROTableAdapter.Fill(Me.CADASTRODataSet.CADASTRO)
        If LoginForm1.GRUPOComboBox.Text = "MASTER" Then
            OPERADORTextBox.ReadOnly = False
        End If
        If LoginForm1.GRUPOComboBox.Text = "MASTER" Then
            OPERADORTextBox.ReadOnly = False
            DELETAR.Enabled = True
            HISTORICORichTextBox.ReadOnly = False
            CADASTROTableAdapter.FillByMENORAGENDA(CADASTRODataSet.CADASTRO, DateValue(Now))
            If CADASTRODataSet.Tables(0).Rows.Count > 0 Then
                MsgBox(LoginForm1.USUARIOTextBox.Text & " EXISTEM " & CADASTRODataSet.Tables(0).Rows.Count & " AGENDAMENTOS ATRASADOS ")
                RAZAOTextBox.Focus()
            Else
                MsgBox(LoginForm1.USUARIOTextBox.Text & " NÃO EXISTE AGENDAMENTOS ATRASADOS")
                RAZAOTextBox.Focus()
            End If
        Else
            CADASTROTableAdapter.FillByAGENDAOPERADOR(CADASTRODataSet.CADASTRO, DateValue(Now), LoginForm1.USUARIOTextBox.Text)
            If CADASTRODataSet.Tables(0).Rows.Count > 0 Then
                MsgBox(LoginForm1.USUARIOTextBox.Text & " EXISTEM " & CADASTRODataSet.Tables(0).Rows.Count & " AGENDAMENTOS ATRASADOS ")
                RAZAOTextBox.Focus()
            Else
                MsgBox(LoginForm1.USUARIOTextBox.Text & " NÃO EXISTE AGENDAMENTOS ATRASADOS")
                RAZAOTextBox.Focus()
            End If
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        EMAILFUNIL.MdiParent = Principal
        EMAILFUNIL.Show()
    End Sub

    Private Sub PRIMEIRO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PRIMEIRO.Click
        CADASTROBindingSource1.MoveFirst()
    End Sub

    Private Sub VOLTAR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VOLTAR.Click
        CADASTROBindingSource1.MovePrevious()
    End Sub

    Private Sub NOVO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NOVO.Click
        CADASTROBindingSource1.AddNew()
        ENDTextBox.Enabled = False
        BAIRROTextBox.Enabled = False
        CIDADETextBox.Enabled = False
        NCONTATOTextBox.Text = 0
        OPERADORTextBox.Text = LoginForm1.USUARIOTextBox.Text
        RAZAOTextBox.Focus()
    End Sub

    Private Sub DELETAR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DELETAR.Click
        If MsgBox("DESEJA DELETAR O REGISTRO?", MsgBoxStyle.OkCancel, "ATENÇÃO") = MsgBoxResult.Ok Then
            Me.Validate()
            CADASTROBindingSource1.RemoveCurrent()
            CADASTROTableAdapter.Update(CADASTRODataSet.CADASTRO)
        End If
    End Sub

    Private Sub FRENTE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FRENTE.Click
        CADASTROBindingSource1.MoveNext()
    End Sub

    Private Sub ULTIMO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ULTIMO.Click
        CADASTROBindingSource1.MoveLast()
    End Sub

    Private Sub SALVAR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SALVAR.Click
        If RAZAOTextBox.Text = "" Or TELEFONETextBox.Text = "" Then
            MsgBox("FAVOR INFORMAR RAZÃO SOCIAL/TELEFONE")
        ElseIf LoginForm1.GRUPOComboBox.Text = "USUARIO" And AGENDAMENTOMaskedTextBox.Text < DateValue(Now) Or AGENDAMENTOMaskedTextBox.Text = "  /  /" Then
            MsgBox("FAVOR INFORMAR UMA DATA DE AGENDAMENTO VÁLIDA")
        ElseIf SITUACAOComboBox.Text = "" Then
            MsgBox("FAVOR INFORMAR A SITUAÇÃO DO CLIENTE", MsgBoxStyle.Information, "ATENÇÃO")
        Else
            If OPERADORTextBox.Text = "" Then
                OPERADORTextBox.Text = LoginForm1.USUARIOTextBox.Text
            Else
                OPERADORTextBox.Text = OPERADORTextBox.Text
            End If
            If RichTextBox1.Text = "" Then
                HISTORICORichTextBox.Text = HISTORICORichTextBox.Text
                ULTCONTATOMaskedTextBox.Text = ULTCONTATOMaskedTextBox.Text
            Else
                HISTORICORichTextBox.Text = HISTORICORichTextBox.Text + "EM " + DateValue(Now) + " POR " + LoginForm1.USUARIOTextBox.Text + Environment.NewLine + RichTextBox1.Text + Environment.NewLine
                NCONTATOTextBox.Text = NCONTATOTextBox.Text + 1
                ULTCONTATOMaskedTextBox.Text = DateValue(Now)
            End If
            Try
                Me.Validate()
                CADASTROBindingSource1.EndEdit()
                TableAdapterManager.UpdateAll(CADASTRODataSet)
                If LoginForm1.GRUPOComboBox.Text = "USUARIO" Then
                    CADASTROTableAdapter.FillByRAZAO(CADASTRODataSet.CADASTRO, RAZAOTextBox.Text)
                    ENDTextBox.Enabled = False
                    BAIRROTextBox.Enabled = False
                    CIDADETextBox.Enabled = False
                End If
                RichTextBox1.Text = ""
            Catch EX As Exception
                MsgBox("ERRO NA ATUALIZAÇÃO DOS DADOS" + EX.Message)
            End Try
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If RAZAOTextBox.Text = "" Then
            MsgBox("FAVOR INFORMAR A RAZÃO SOCIAL")
        Else
            CADASTROTableAdapter.FillByRAZAO(CADASTRODataSet.CADASTRO, RAZAOTextBox.Text)
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        If ULTCONTATOMaskedTextBox.Text = "  /  /" Then
            MsgBox("FAVOR INFORMAR A DATA DO ÚLTIMO CONTATO")
        Else
            CADASTROTableAdapter.FillByULTCONTATO(CADASTRODataSet.CADASTRO, ULTCONTATOMaskedTextBox.Text)
        End If
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        CADASTROTableAdapter.FillByPRAHJ(CADASTRODataSet.CADASTRO, DateValue(Now))
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        If AGENDAMENTOMaskedTextBox.Text = "  /  /" Then
            MsgBox("FAVOR INFORMAR A DATA DO AGENDAMENTO")
        Else
            CADASTROTableAdapter.FillByAGENDAMENTO(CADASTRODataSet.CADASTRO, AGENDAMENTOMaskedTextBox.Text)
        End If
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        If DATAVISITAMaskedTextBox.Text = "  /  /" Then
            MsgBox("FAVOR INFORMAR A DATA DA VISITA")
        Else
            CADASTROTableAdapter.FillByDATAVISITA(CADASTRODataSet.CADASTRO, DATAVISITAMaskedTextBox.Text)
        End If
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        If LoginForm1.GRUPOComboBox.Text = "MASTER" Then
            CADASTROTableAdapter.FillByOPERADOR(CADASTRODataSet.CADASTRO, OPERADORTextBox.Text)
        Else
            CADASTROTableAdapter.FillByOPERADOR(CADASTRODataSet.CADASTRO, LoginForm1.USUARIOTextBox.Text)
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        CADASTROTableAdapter.Fill(CADASTRODataSet.CADASTRO)
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        Dim CalForm As New Form
        Dim Cal As New MonthCalendar
        Cal.MaxSelectionCount = 1
        CalForm.Controls.Add(Cal)
        CalForm.ClientSize = Cal.Size
        CalForm.Height += 24
        CalForm.Width += 50
        CalForm.MinimizeBox = False
        CalForm.MaximizeBox = False
        CalForm.MinimumSize = CalForm.Size
        CalForm.MaximumSize = CalForm.Size
        CalForm.ShowDialog()
        AGENDAMENTOMaskedTextBox.Text = Cal.SelectionRange.Start.ToString
        CalForm.Close()
        CalForm.Dispose()
        CalForm = Nothing
        Cal.Dispose()
        Cal = Nothing
    End Sub
    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked = True Then
            ENDTextBox.Enabled = True
            CIDADETextBox.Enabled = True
            BAIRROTextBox.Enabled = True
        Else
            ENDTextBox.Enabled = False
            CIDADETextBox.Enabled = False
            BAIRROTextBox.Enabled = False
        End If
    End Sub

    Private Sub VISITAAGENComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VISITAAGENComboBox.SelectedIndexChanged
        If VISITAAGENComboBox.Text = "SIM" Then
            DATAVISITAMaskedTextBox.Enabled = True
        Else
            DATAVISITAMaskedTextBox.Enabled = False
        End If
    End Sub

    Private Sub CEPMaskedTextBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles CEPMaskedTextBox.LostFocus
        BuscaCep(CEPMaskedTextBox.Text)
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        MAPS.MdiParent = Principal
        MAPS.Show()
    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        If SITUACAOComboBox.Text = "" Then
            MsgBox("FAVOR INFORMAR A SITUAÇÃO")
        Else
            CADASTROTableAdapter.FillBySITUACAO(CADASTRODataSet.CADASTRO, SITUACAOComboBox.Text)
        End If
    End Sub

    Private Sub OPERADORTextBox_TextChanged(sender As Object, e As System.EventArgs) Handles OPERADORTextBox.TextChanged
        If LoginForm1.GRUPOComboBox.Text = "USUARIO" And OPERADORTextBox.Text = LoginForm1.USUARIOTextBox.Text Then
            DELETAR.Enabled = True
        ElseIf LoginForm1.GRUPOComboBox.Text = "USUARIO" And OPERADORTextBox.Text <> LoginForm1.USUARIOTextBox.Text Then
            DELETAR.Enabled = False
        ElseIf LoginForm1.GRUPOComboBox.Text = "MASTER" Then
            DELETAR.Enabled = True
        End If
    End Sub

    Private Sub Button12_Click(sender As System.Object, e As System.EventArgs) Handles Button12.Click
        CADASTROTableAdapter.FillByAGENDAOPERADOR(CADASTRODataSet.CADASTRO, DateValue(Now), LoginForm1.USUARIOTextBox.Text)
    End Sub

    Private Sub RAZAOTextBox_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles RAZAOTextBox.KeyDown
        Dim RAZAO As String
        If e.KeyCode = Keys.Enter Then
            RAZAO = RAZAOTextBox.Text
            CADASTROTableAdapter.FillByRAZAO(CADASTRODataSet.CADASTRO, RAZAOTextBox.Text)
            If CADASTRODataSet.Tables(0).Rows.Count > 0 Then
                MsgBox("EMPRESA JÁ CADASTRADA")
            ElseIf MsgBox("EMPRESA NÃO CADASTRADA,DESEJA CADASTRALA AGORA?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                CADASTROBindingSource1.AddNew()
                RAZAOTextBox.Text = RAZAO
                ENDTextBox.Enabled = False
                BAIRROTextBox.Enabled = False
                CIDADETextBox.Enabled = False
                NCONTATOTextBox.Text = 0
                OPERADORTextBox.Text = LoginForm1.USUARIOTextBox.Text
                RAZAOTextBox.Focus()
            End If
        End If
    End Sub
End Class